package com.vipheyue.lanjingapp;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.vipheyue.lanjingapp.bean.MyUser;
import com.vipheyue.lanjingapp.fragment.ReNameFragment0;
import com.vipheyue.lanjingapp.fragment.ReNameFragment1;
import com.vipheyue.lanjingapp.fragment.ReNameFragment2;
import com.vipheyue.lanjingapp.fragment.ReNameFragment3;
import com.vipheyue.lanjingapp.utils.DoubleClickExitDetector;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.listener.BmobDialogButtonListener;
import cn.bmob.v3.update.BmobUpdateAgent;
import cn.bmob.v3.update.UpdateStatus;

public class MainActivity extends FragmentActivity   implements NavigationView.OnNavigationItemSelectedListener {
    /**
     * 1.实例化各类控件
     */


    @Bind(R.id.content)
    LinearLayout content;


    private List<Fragment> fragmentArrayList;
    private FragmentManager fm;
    private Fragment currentFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_layout);
        ButterKnife.bind(this);
        BmobUpdateAgent.update(this);
        BmobUpdateAgent.setUpdateOnlyWifi(false);
        //设置对对话框按钮的点击事件的监听
        BmobUpdateAgent.setDialogListener(new BmobDialogButtonListener() {

            @Override
            public void onClick(int status) {
                // TODO Auto-generated method stub
                switch (status) {
                    case UpdateStatus.Close://只有在强制更新状态下才会在更新对话框的右上方出现close按钮,如果用户不点击”立即更新“按钮，这时候开发者可做些操作，比如直接退出应用等
                        finish();
                        break;
                }
            }
        });
        
        initView();
        showFragmentIndex();
//        StatusBarCompat.compat(this, 0xFF00ABEC);
        initNaviView();

    }
    private void showFragmentIndex() {
        fm = getSupportFragmentManager();
        currentFragment=fragmentArrayList.get(0);
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.content, fragmentArrayList.get(0));
        ft.commit();
    }
    private void initNaviView() {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
      /*  fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });*/

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        TextView nav_header_email=ButterKnife.findById(navigationView, R.id.nav_header_email);
        MyUser userInfo = BmobUser.getCurrentUser(this,MyUser.class);
        if(userInfo != null){
            nav_header_email.setText(userInfo.getEmail());
        }else{
            nav_header_email.setText("未登录");
        }
    }

    private void initView() {
        fragmentArrayList = new ArrayList<Fragment>();
        fragmentArrayList.add(new ReNameFragment0());
        fragmentArrayList.add(new ReNameFragment1());
        fragmentArrayList.add(new ReNameFragment2());
        fragmentArrayList.add(new ReNameFragment3());
    }

    @OnClick({R.id.home_radioBtn_rename0, R.id.home_radioBtn_rename3})
    public void onClick(View view) {
        FragmentTransaction ft = fm.beginTransaction();
        switch (view.getId()) {
            case R.id.home_radioBtn_rename0:
                switchContent(fragmentArrayList.get(0));
                break;
            case R.id.home_radioBtn_rename3:
                switchContent(fragmentArrayList.get(3));
                break;
        }
        ft.commit();
    }



    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);

        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
    public void switchContent( Fragment to) {
        if (currentFragment != to) {
//            FragmentTransaction transaction = fm.beginTransaction().setCustomAnimations(
//                    android.R.anim.fade_in, R.anim.slide_out_to_bottom);
            FragmentTransaction transaction = fm.beginTransaction();
            if (!to.isAdded()) {    // 先判断是否被add过
                transaction.hide(currentFragment).add(R.id.content, to).commit(); // 隐藏当前的fragment，add下一个到Activity中
            } else {
                transaction.hide(currentFragment).show(to).commit(); // 隐藏当前的fragment，显示下一个
            }
            currentFragment = to;
        }
    }
    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_camera) {
            // Handle the camera action
        } else if (id == R.id.nav_gallery) {
            Toast.makeText(this, "gallery", Toast.LENGTH_SHORT).show();
        } else if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }



}